//
//  KLHistoryApplyKeyListTableViewController.m
//  KISSLOCK
//
//  Created by 陈宾鹏 on 16/10/18.
//  Copyright © 2016年 陈宾鹏. All rights reserved.
//

#import "KLHistoryApplyKeyListTableViewController.h"
#import "KLHelp.h"
#import "KLHttpAPI.h"
#import "HemaConst.h"
#import "Masonry.h"
#import "UIActionSheet+Blocks.h"

@interface KLHistoryApplyKeyListTableViewController ()
@property(nonatomic,strong) NSArray *allHistoryListArr;
@end

@implementation KLHistoryApplyKeyListTableViewController
-(NSArray *)allHistoryListArr{
    if(!_allHistoryListArr){
        _allHistoryListArr = [NSArray array];
    }
    return _allHistoryListArr;
}

-(void)viewWillAppear:(BOOL)animated{
    [self refresh];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    [UIApplication sharedApplication].statusBarHidden = YES;
    
    self.tableView.tableFooterView = [[UIView alloc]init];
    
}

#pragma mark - Table view data source
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.allHistoryListArr.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"historyApplyList"];
    if(!cell){
        cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"historyApplyList"];
    }else{
        //删除cell的所有子视图
        while ([cell.contentView.subviews lastObject] != nil){
            [(UIView*)[cell.contentView.subviews lastObject] removeFromSuperview];
        }
    }
    
    NSDictionary *singleMessage = self.allHistoryListArr[indexPath.row];
    //申请人的Label
    UILabel *applyPersonLabel = [[UILabel alloc]init];
    applyPersonLabel.text = [NSString stringWithFormat:@"锁主:%@  预付:%@元",[singleMessage objectForKey:@"nickname"],[singleMessage objectForKey:@"single_price"]];
    applyPersonLabel.font = [UIFont systemFontOfSize:16];
    applyPersonLabel.textColor = RGB_UI_COLOR(90, 90, 90);
    [cell.contentView addSubview:applyPersonLabel];
    [applyPersonLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(15);
        make.left.mas_equalTo(35);
    }];
    //锁名
    UILabel *lockNameLabel = [[UILabel alloc]init];
    lockNameLabel.text = [NSString stringWithFormat:@"锁名: %@",[singleMessage objectForKey:@"name"]];
    lockNameLabel.font = [UIFont systemFontOfSize:14];
    lockNameLabel.textColor = [UIColor lightGrayColor];
    [cell.contentView addSubview:lockNameLabel];
    [lockNameLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(applyPersonLabel).with.offset(21);
        make.left.mas_equalTo(35);
    }];
    //有效期
    UILabel *dateLabel = [[UILabel alloc]init];
    dateLabel.numberOfLines = 2;
    dateLabel.text = [NSString stringWithFormat:@"有效期: %@ \n       至: %@",[singleMessage objectForKey:@"startdate"],[singleMessage objectForKey:@"enddate"]];
    dateLabel.font = [UIFont systemFontOfSize:14];
    dateLabel.textColor = [UIColor lightGrayColor];
    [cell.contentView addSubview:dateLabel];
    [dateLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(lockNameLabel).with.offset(18);
        make.left.mas_equalTo(35);
        make.height.mas_equalTo(35);
        make.right.mas_equalTo(-10);
    }];
    //申请时间
    UILabel *applyTime = [[UILabel alloc]init];
    applyTime.text = [NSString stringWithFormat:@"申请时间:%@",[singleMessage objectForKey:@"regdate"]];
    applyTime.lineBreakMode = NSLineBreakByTruncatingTail;
    applyTime.numberOfLines = 0;
    applyTime.font = [UIFont systemFontOfSize:14];
    applyTime.textColor = RGB_UI_COLOR(164, 194, 244);
    [cell.contentView addSubview:applyTime];
    [applyTime mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(dateLabel).with.offset(35);
        make.right.mas_equalTo(-45);
    }];
    //锁主手机号
    UILabel *mobileLabel = [[UILabel alloc]init];
    mobileLabel.text = [NSString stringWithFormat:@"锁主手机号:%@",[singleMessage objectForKey:@"master_mobile"]];
    mobileLabel.lineBreakMode = NSLineBreakByTruncatingTail;
    mobileLabel.numberOfLines = 0;
    mobileLabel.font = [UIFont systemFontOfSize:14];
    mobileLabel.textColor = RGB_UI_COLOR(164, 194, 244);
    [cell.contentView addSubview:mobileLabel];
    [mobileLabel mas_makeConstraints:^(MASConstraintMaker *make) {
//        make.top.equalTo(dateLabel).with.offset(43);
        make.bottom.mas_equalTo(-5);
        make.right.mas_equalTo(-45);
    }];
    //过期申请不可点击
    NSString *status = [singleMessage objectForKey:@"status"];
    if(status.integerValue != 0 && status.integerValue != 3){
        cell.userInteractionEnabled = NO;
        cell.backgroundColor = RGB_UI_COLOR(240, 240, 230);
    }else{
        cell.backgroundColor = [UIColor clearColor];
    }
    
    //标示信息
    UILabel *statusLabel = [[UILabel alloc]init];
    statusLabel.numberOfLines = 0;
    statusLabel.font = [UIFont systemFontOfSize:17];
    [cell.contentView addSubview:statusLabel];
    [statusLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.width.mas_equalTo(20);
        make.left.mas_equalTo(10);
        make.centerY.equalTo(cell.contentView);
    }];
    if(status.integerValue == 1){//同意
        statusLabel.text = @"已同意";
        statusLabel.textColor = RGB_UI_COLOR(102, 255, 153);
    }else if (status.integerValue == 2){//拒绝
        statusLabel.text = @"已拒绝";
        statusLabel.textColor = RGB_UI_COLOR(255, 102, 102);
    }else if (status.integerValue == 4 || status.integerValue == 5){//过期
        statusLabel.text = @"已过期";
        statusLabel.textColor = RGB_UI_COLOR(255, 242, 0);
    }else if (status.integerValue == 6){
        statusLabel.text = @"已撤消";
        statusLabel.textColor = RGB_UI_COLOR(230, 145, 56);
    }
    
    return cell;
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    UIActionSheet *sheet = [[UIActionSheet alloc]initWithTitle:@"撤消该条申请？" cancelButtonItem:[RIButtonItem itemWithLabel:@"不撤销"] destructiveButtonItem:[RIButtonItem itemWithLabel:@"撤消" action:^{
        [KLHttpAPI requestMasterIsAgreedWithKey_str:@"" AESkey:@"" power:@"" Command_code:@"" updatetime:@"" news_id:self.allHistoryListArr[indexPath.row] status:@"6" completionHandler:^(MKNetworkOperation *operation) {
            NSString *isSuccess = [operation.responseJSON objectForKey:@"success"];
            if(isSuccess.integerValue == 1){
                [self refresh];
            }else{
                [KLHelp showTextHUDforView:self.view text:[operation.responseJSON objectForKey:@"msg"]];
            }
        } errorHandler:^(NSString *error) {
        }];
//        [KLHttpAPI requestMasterIsAgreedWithKey_str:@"" news_id:[self.allHistoryListArr[indexPath.row] objectForKey:@"id"] status:@"6" completionHandler:^(MKNetworkOperation *operation) {
//            NSString *isSuccess = [operation.responseJSON objectForKey:@"success"];
//            if(isSuccess.integerValue == 1){
//                [self refresh];
//            }else{
//                [KLHelp showTextHUDforView:self.view text:[operation.responseJSON objectForKey:@"msg"]];
//            }
//        } errorHandler:^(NSString *error) {
//            
//        }];
    }] otherButtonItems: nil];
    [sheet showInView:self.view];
}

-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    return 75.0f;
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 130.0f;
}

-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
    UIButton *iKnow = [[UIButton alloc]initWithFrame:CGRectMake(UI_View_Width/2-30, 30, 90, 30)];
    [iKnow setTitle:@"知道了" forState:UIControlStateNormal];
    [iKnow setTitleColor:RGB_UI_COLOR(102, 255, 102) forState:UIControlStateNormal];
    iKnow.layer.masksToBounds = YES;
    iKnow.layer.cornerRadius = 5;
    iKnow.layer.borderColor = RGB_UI_COLOR(102, 255, 102).CGColor;
    iKnow.layer.borderWidth = 0.5;
    [iKnow addTarget:self action:@selector(dismissViewController:) forControlEvents:UIControlEventTouchUpInside];
    
    UIView *line = [[UIView alloc]initWithFrame:CGRectMake(0, 74, UI_View_Width, 1)];
    line.backgroundColor = RGB_UI_COLOR(74, 134, 232);
    
    UIView *fatherView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, UI_View_Width, 75)];
    fatherView.backgroundColor = [UIColor whiteColor];
    [fatherView addSubview:iKnow];
    [fatherView addSubview:line];
    
    return fatherView;
}

-(void)dismissViewController:(id)sender{
    [self dismissViewControllerAnimated:YES completion:nil];
}

-(void)refresh{
    [KLHelp showHUDAddedTo:self.view animated:YES];
    [KLHttpAPI requestHaveApplyedForKeyListWithCompletionHandler:^(MKNetworkOperation *operation) {
        [self.view endEditing:YES];
        [KLHelp hideAllHUDsForView:self.view animated:YES];
        NSLog(@"获取申请钥匙的历史记录:%@",operation.responseJSON);
        NSString *isSuccess = [operation.responseJSON objectForKey:@"success"];
        if(isSuccess.integerValue == 1){
            self.allHistoryListArr = [NSArray arrayWithArray:[operation.responseJSON objectForKey:@"infor"]];
            [self.tableView reloadData];
            if(self.allHistoryListArr.count == 0){
                [KLHelp setNoDateImageWithView:self.view type:0];
            }else{
                [KLHelp removeImgViewAndNoticeLabel];
            }
        }
    } errorHandler:^(NSString *error) {
        [KLHelp hideAllHUDsForView:self.view animated:YES];
    }];
}
@end
